/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2015
 */

package com.environment_pro.monitor.service.impl;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.environment_pro.monitor.mapper.TbSystemUserMapper;
import com.environment_pro.monitor.model.TbSystemUser;
import com.environment_pro.monitor.service.TbSystemUserService;
import com.environment_pro.monitor.util.PageHelper;
import com.environment_pro.monitor.util.PageInfo;
import com.environment_pro.monitor.util.PageHelper.Page;
import com.environment_pro.monitor.util.StaticUtil;

/**
 * @author yangfeng 
 * @version 1.0
 * @since 1.0
 */

@Service
@Transactional  //此处不再进行创建SqlSession和提交事务，都已交由spring去管理了。
public class TbSystemUserServiceImpl implements TbSystemUserService {
	
	@Resource
	private TbSystemUserMapper mapper;

	//移除缓存
	//@CacheEvict(value = "tbSystemUserCache",key="'id'+#id")
	public boolean delete(int id) {
		
		return mapper.delete(id);
	}

	@SuppressWarnings("unchecked")
	public Page<TbSystemUser> findAll(PageInfo pageInfo, TbSystemUser tbSystemUser) {
		PageHelper.startPage(pageInfo);  
		mapper.findAll(tbSystemUser);
		return PageHelper.endPage();
	}

	//使用缓存
	//@Cacheable(value = "tbSystemUserCache",key="'id'+#id")
	public TbSystemUser findById(int id) {
		TbSystemUser tbSystemUser = mapper.findById(id);
		return tbSystemUser;
	}
	
	//使用缓存
	@Cacheable(value = "tbSystemUserCache",key="'code'+#username")
	public TbSystemUser findByUsername(String username) {
		System.out.println("tbSystemUserCache 1:"+username);
		TbSystemUser tbSystemUser = mapper.findByUsername(username);
		System.out.println("tbSystemUserCache 2:"+username);
		return tbSystemUser;
	}
	
	//缓存数据
	//@CachePut(value = "tbSystemUserCache",key="'id'+#tbSystemUser.id")
	public void save(TbSystemUser tbSystemUser) {
		tbSystemUser.setAddTime(new Date());
		mapper.save(tbSystemUser);
	}
	//更新缓存
	//@CacheEvict(value = "tbSystemUserCache",key="'id'+#tbSystemUser.id")
	public boolean update(TbSystemUser tbSystemUser) {

		return mapper.update(tbSystemUser);
	}

	//编码转化为名称
	public String code2name(String username) {
		TbSystemUser tbSystemUser = (TbSystemUser)StaticUtil.getCacheObject("tbSystemUserCache", String.valueOf("code"+username));
		if (tbSystemUser==null) {
			tbSystemUser = this.findByUsername(username);
			StaticUtil.putCacheObject("tbSystemUserCache", String.valueOf("code"+username), tbSystemUser);
		}
		return tbSystemUser.getRealName();
	}

	//ID转化为名称
	public String id2name(int id) {
		TbSystemUser tbSystemUser = (TbSystemUser)StaticUtil.getCacheObject("tbSystemUserCache", String.valueOf("id"+id));
		if (tbSystemUser==null) {
			tbSystemUser = this.findById(id);
			StaticUtil.putCacheObject("tbSystemUserCache", String.valueOf("id"+id), tbSystemUser);
		}
		return tbSystemUser.getRealName();
	}

}